<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS定位示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            padding: 20px;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
        }

        .demo-box {
            margin: 20px;
            padding: 20px;
            border: 2px solid #3498db;
            background-color: #f8f9fa;
            position: relative;
            height: 300px;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: #3498db;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
        }

        /* 相对定位 */
        .relative {
            position: relative;
            top: 20px;
            left: 20px;
            background-color: #e74c3c;
        }

        /* 绝对定位 */
        .absolute {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #2ecc71;
        }

        /* 固定定位 */
        .fixed {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: #f1c40f;
        }

        /* 粘性定位 */
        .sticky-container {
            height: 200px;
            overflow-y: scroll;
            border: 2px solid #3498db;
            margin: 20px;
        }

        .sticky {
            position: sticky;
            top: 0;
            background-color: #9b59b6;
            padding: 10px;
            margin: 0;
            z-index: 1;
        }

        .content {
            height: 400px;
            padding: 20px;
            background-color: #f8f9fa;
        }

        /* Z-index示例 */
        .z-index-demo {
            position: relative;
            height: 200px;
            margin: 20px;
            border: 2px solid #3498db;
        }

        .z-box {
            position: absolute;
            width: 100px;
            height: 100px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }

        .z-box-1 {
            background-color: rgba(231, 76, 60, 0.8);
            top: 20px;
            left: 20px;
            z-index: 1;
        }

        .z-box-2 {
            background-color: rgba(46, 204, 113, 0.8);
            top: 40px;
            left: 40px;
            z-index: 2;
        }

        .z-box-3 {
            background-color: rgba(52, 152, 219, 0.8);
            top: 60px;
            left: 60px;
            z-index: 3;
        }

        .description {
            margin: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 4px;
            line-height: 1.6;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>CSS定位详解</h1>

        <section>
            <h2>1. 相对定位（relative）</h2>
            <div class="description">
                相对定位是相对于元素原本的位置进行偏移，不会脱离文档流。
            </div>
            <div class="demo-box">
                <div class="box relative">相对定位</div>
            </div>
        </section>

        <section>
            <h2>2. 绝对定位（absolute）</h2>
            <div class="description">
                绝对定位是相对于最近的定位祖先元素进行定位，会脱离文档流。
            </div>
            <div class="demo-box">
                <div class="box absolute">绝对定位</div>
            </div>
        </section>

        <section>
            <h2>3. 固定定位（fixed）</h2>
            <div class="description">
                固定定位是相对于浏览器视口进行定位，不会随页面滚动而移动。
            </div>
            <div class="demo-box">
                <div class="box fixed">固定定位</div>
            </div>
        </section>

        <section>
            <h2>4. 粘性定位（sticky）</h2>
            <div class="description">
                粘性定位在滚动到特定位置时会固定在视口中。
            </div>
            <div class="sticky-container">
                <h3 class="sticky">粘性标题</h3>
                <div class="content">
                    <p>滚动此区域查看粘性定位效果</p>
                    <p>继续滚动...</p>
                    <p>粘性定位的元素会在到达指定位置时固定。</p>
                </div>
            </div>
        </section>

        <section>
            <h2>5. Z-index层叠顺序</h2>
            <div class="description">
                z-index控制定位元素的层叠顺序，数值越大越靠前。
            </div>
            <div class="z-index-demo">
                <div class="z-box z-box-1">z-index: 1</div>
                <div class="z-box z-box-2">z-index: 2</div>
                <div class="z-box z-box-3">z-index: 3</div>
            </div>
        </section>
    </div>
</body>
</html>
